<?php
namespace App\Http\Controllers\Enterprise;

use App\Http\Controllers\BaseApiController;
use App\Services\Enterprise\EnterpriseService;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Validator;

/**
 * Created by PhpStorm.
 * User: 1
 * Date: 2019/2/19
 * Time: 16:42
 */
class EnterpriseController extends BaseApiController
{
    private $service;

    public function __construct(EnterpriseService $service)
    {
        //继承基类的构造方法
        parent::__construct();
        //依赖注入service
        $this->service=$service;
    }
    public function add(Request $request){
        //写一些验证规则
        //验证参数
        //controller 去service调用逻辑代码
        //封装一个
        //编写验证规则
        $rule = [
            'enterprise_name' => 'required|max:20',
            'enterprise_logo'=>'required|max:200',
            'enterprise_business_license'=>'required|max:100',
            'enterprise_corporate_name'=>'required|max:50',
            'enterprise_credit_code'=>'required|max:100',
            'enterprise_industry'=>'required|max:20',
            'enterprise_unit_size'=>'required|max:200',
            'enterprise_unit_address'=>'required|max:200',
            'enterprise_company_industry'=>'required|max:200',
            'enterprise_mailbox'=>'required|max:200|email',
            'enterprise_company_profile'=>'required|max:200',
            'enterprise_enterprise_video'=>'max:20',
            'enterprise_image'=>'required|max:200'
        ];
        if ($error = $this->validate($rule,$request)){
            return $error;
        }
        return $this->service->add($request);
    }

    /**
     * 修改企业信息
     */
    public function save(Request $request){
        //return $request;
        //编写验证规则
        $rule = [
            'id'=>'required|max:100000|integer',
            'enterprise_corporate_name'=>'required|max:50',
            'enterprise_credit_code'=>'required|max:100',
            'enterprise_industry'=>'required|max:20|string',
            'enterprise_unit_size'=>'required|max:200',
            'enterprise_unit_address'=>'required|max:200|string',
            'enterprise_mailbox'=>'required|max:200|email',
            'enterprise_company_profile'=>'required|max:200',
            'enterprise_company_industry'=>'required|max:200|string',
            'enterprise_business_license'=>'required|max:100000000|integer',
        ];
        if ($error = $this->validate($rule,$request)){
            return $error;
        }
        //传入公司id,执行查询
        return $this->service->save($request);
    }
    /**
     * 查询单条信息
     */
    public function sel(Request $request){
        //查询当前公司的信息
        //编写验证规则
        $rule = ['id'=>'required|max:1000000|integer'];
        //验证是否验证成功
        //传入rule和接受的数据
        if ($error = $this->validate($rule,$request)){
            return $error;
        }
        //传入公司id,执行查询
        return $this->service->sel($request->get('id'));
    }
    /**
     * 查询所有的热门企业
     */
    public function selall(){
        //查询所有的热门企业
        return $this->service->selall();
    }
    //没有删除的不用补全
    public function del(){

    }

    /**
     * 企业排序
     */
    public function order(Request $request){
        //排序类型(1为正序,2为逆序)
        $rule = [
            'type'=>'required|max:2|integer'
        ];
        //传入rule和接受的数据
        if ($error = $this->validate($rule,$request)){
            return $error;
        }
        //执行查询
        return $this->service->order($request);
    }
    /**
     * 图片上传
     */
    public function uploadImg(Request $request){
        //上传完成,修改数据
        return $this->service->save($request);
    }
    /**
     * 视频上传
     */
    public function uploadVideo(Request $request){
        //上传完成,修改数据
        return $this->service->upload($request);
    }
}